package com.mrbeard.greenbroswer.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import com.mrbeard.greenbroswer.entites.Device;
import com.mrbeard.greenbroswer.entites.Log;
import com.mrbeard.greenbroswer.utils.DeviceUtil;
import com.mrbeard.greenbroswer.utils.FileUtil;
import com.mrbeard.greenbroswer.utils.FormatUtil;
import com.mrbeard.greenbroswer.utils.RabbitmqUtil;

import java.io.IOException;
import java.util.List;

/**
 * Created by 胡彬 on 2018/5/15.
 */
public class MyReceiver extends BroadcastReceiver{
    String logs = null;
    Device device = new Device();
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
       // Log.d("MyTag", "onclock......................");
        String msg = intent.getStringExtra("msg");
        Toast.makeText(context,"发送日志成功!",Toast.LENGTH_LONG).show();
        if(msg.equals("sendLogs")){
            //Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
            sendLog(context);
        }
    }

    /**
     * 发送日志
     *
     */
    public void sendLog(Context context) {

        DeviceUtil deviceUtil = new DeviceUtil(context);
        device = deviceUtil.getDevice();
        try {
            FileUtil fileUtil = new FileUtil();
            logs = fileUtil.getFileContent(".accesstrace.dat");
        } catch (IOException e) {
            e.printStackTrace();
        }


        if (logs == null) {
            android.util.Log.e("logs2", "未获取到内容");
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    List<Log> logList = FormatUtil.formatLogs(logs, device.getImei());
                    RabbitmqUtil rabbitmqUtil = new RabbitmqUtil("topicExchage", "topic.queue3", "118.89.52.198", "topic", device.getImei(), logList.toString());
                    rabbitmqUtil.send();
                }
            }).start();

        }
    }
}
